package com.ws.shop.dao.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Set;

import org.apache.log4j.Logger;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.ResultSetExtractor;
import org.springframework.jdbc.core.StatementCallback;
import org.springframework.stereotype.Repository;

import com.ws.shop.dao.AsynGetTreeMenuDao;
import com.ws.shop.entity.AdminMenu;
import com.ws.shop.util.JdbcUtil;
@Repository(value="asynGetTreeMenuDao")
public class AsynGetTreeMenuDaoImpl extends BaseDaoImpl implements AsynGetTreeMenuDao {
	private static Logger log = Logger.getLogger(AsynGetTreeMenuDaoImpl.class);

	private final String GET_TREE_MENU_SQL = new StringBuilder("select b.menuid,b.menuname,b.menutype,b.linkaddr,b.pmenuid,b.ord from admin_menu b,admin_role_menu r,admin_role br where b.menuid=r.menuid and r.roleid=br.roleid and br.status=1 and br.roleid=? and b.status=1 and b.menutype=1 order by b.ord").toString();
	public  Map<Integer, AdminMenu> getTreemenuByRoleid(final int roleid){
		return this.query(GET_TREE_MENU_SQL, new ResultSetExtractor<Map<Integer, AdminMenu>>(){
			public Map<Integer, AdminMenu> extractData(ResultSet rs)
					throws SQLException, DataAccessException {
				Map<Integer, AdminMenu> menumap=new LinkedHashMap<Integer, AdminMenu>();
				AdminMenu b = null;
				int menuid=-1;
				while(rs.next()){
					b=new AdminMenu();
					menuid=rs.getInt(1);
					b.setMenuid(menuid);
					b.setMenuname(rs.getString(2));
					b.setMenutype(rs.getInt(3));
					b.setLinkaddr(rs.getString(4));
					b.setPmenuid(rs.getInt(5));
					b.setOrd(rs.getInt(6));
					menumap.put(menuid, b);
				}
				Set<Integer> set =menumap.keySet();
				AdminMenu pb=null;
				for(Iterator<Integer> it=set.iterator();it.hasNext();){
					menuid=it.next();
					b=menumap.get(menuid);
					if(b.getPmenuid()>0){
						pb=menumap.get(b.getPmenuid());
						if(pb!=null){
							pb.getChildren().add(b);
						}
					}
				}
				return menumap;
			}
		}, roleid);
	}
	
	
	private final String GET_ALL_TREE_MENU_SQL = new StringBuilder("select menuid,menuname,menutype,linkaddr,pmenuid,ord from admin_menu where status=1 and menutype=1 order by ord").toString();
	@SuppressWarnings("unchecked")
	public Map<Integer, AdminMenu> getalltreemenu() {
		return (Map<Integer, AdminMenu>)this.execute(new StatementCallback(){
			public Object doInStatement(Statement stmt) throws SQLException,
					DataAccessException {
				ResultSet rs=null;
				Map<Integer, AdminMenu> menumap=new LinkedHashMap<Integer, AdminMenu>();
				try{
					rs=stmt.executeQuery(GET_ALL_TREE_MENU_SQL);
					AdminMenu b=null;
					int menuid=-1;
					while(rs.next()){
						b=new AdminMenu();
						menuid=rs.getInt(1);
						b.setMenuid(menuid);
						b.setMenuname(rs.getString(2));
						b.setMenutype(rs.getInt(3));
						b.setLinkaddr(rs.getString(4));
						b.setPmenuid(rs.getInt(5));
						menumap.put(menuid, b);
					}
					Set<Integer> set =menumap.keySet();
					AdminMenu pb=null;
					for(Iterator<Integer> it=set.iterator();it.hasNext();){
						menuid=it.next();
						b=menumap.get(menuid);
						if(b.getPmenuid()>0){
							pb=menumap.get(b.getPmenuid());
							if(pb!=null){
								pb.getChildren().add(b);
							}
						}
					}
				}catch(Exception e){
					log.error("获取菜单失败AsynGetTreeMenuUtil.getalltreemenu",e);
				}finally{
					JdbcUtil.close(rs, null, null);
				}
				return menumap;
			}
		});
	}

}
